package com.xunshibao.tutor.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xunshibao.tutor.po.School;

@Controller
@RequestMapping(value = "/school/*")
public class SchoolController {
	@Autowired
	private SchoolServiceImpl service;

	@RequestMapping(value = "get")
	public @ResponseBody
	List<Map<String, Object>> getUniv(@RequestParam(value = "p") Integer pid,
			@RequestParam(value = "tp") Integer type) {
		List<School> schools = null;
		switch (type) {
		case 0:
			schools = service.listUiversityByProvince(pid);
			break;
		case 1:
			schools = service.listHighSchoolByDist(pid);
			break;
		case 2:
			schools = service.listCollegeByDist(pid);
			break;
		case 3:
			schools = service.listJuniorSchoolByDist(pid);
			break;
		}

		return school2Map(schools);
	}

	private List<Map<String, Object>> school2Map(List<School> schools) {
		if (schools != null) {
			List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = null;
			School school;
			Iterator<School> iter = schools.iterator();
			while (iter.hasNext()) {
				school = iter.next();
				map = new HashMap<String, Object>();
				map.put("n", school.getName());
				map.put("c", school.getCode());
				iter.remove();
				listMap.add(map);
			}
			return listMap;
		} else {
			return Collections.emptyList();
		}
	}
}
